{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c4d07425",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "> * 1.list的一般方法/函数的使用案例，如\n",
    ">> 1.append() 增加，增加list元素到list的末尾，常用语新建列表\n",
    ">> 2.pop(index)/remove(values)对比两者优势\n",
    ">> 3.count()查看某个元素出现的次数\n",
    ">> 4.index() 查看某个元素第一次出现的位置\n",
    ">> 5.extend() 两个list合并在一起，也可以用[]+[]来实现\n",
    ">> 6.sort() list元素的排列\n",
    "\n",
    "> * 2. list的高级用法：如\n",
    ">> 1.copy()list(可变的数据类型)复制 不等价于赋值，可用[:]来实现\n",
    ">> 2.enumerate()枚举，可同时遍历并获取index和values\n",
    "\n",
    "\n",
    "# 本周内容\n",
    "## list练习之\n",
    "### 初始字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "38d3b838",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-18T07:37:18.657366Z",
     "start_time": "2021-10-18T07:37:18.649070Z"
    }
   },
   "outputs": [],
   "source": [
    "#> * vowels.py\n",
    "vowels = ['a','e','i','o','u']\n",
    "word = \"Milliways\"\n",
    "#   for 语句 第一个元素letter不能和已有变量重名\n",
    "            # 第一个元素一般不定义名字，通常用item  元素 （i）命名\n",
    "            # 第二个元素word（重要），意味着将遍历的内容\n",
    "            # 遍历的内容一般是 序列数据 str / list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6f1389a6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-18T07:41:42.111691Z",
     "start_time": "2021-10-18T07:41:42.096121Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['i', 'a']\n"
     ]
    }
   ],
   "source": [
    "#> * vowels2.py\n",
    "\n",
    "found = []\n",
    "for letter in word:\n",
    "#     print(letter)\n",
    "    if letter in vowels:# letter 在 元音字母表中\n",
    "        if letter not in found:# letter 不在 found表中\n",
    "            found.append(letter)\n",
    "        \n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f427099c",
   "metadata": {},
   "source": [
    "用户输入、打印信息、\n",
    "逻辑：购物车 比较 、append 增加\n",
    "循环 for  or while Ture  商品列表。。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a76bc902",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-18T08:13:02.707512Z",
     "start_time": "2021-10-18T08:12:59.436706Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您支付宝余额：66\n",
      "请选择你喜欢的商品：\n",
      "商品列表:\n",
      "火鸡面 6元\n",
      "王中王 2元\n",
      "爽口豆干 1.5元\n",
      "百事可乐 3.5元\n",
      "可口可乐 3.5元\n",
      "冰红茶 3元\n",
      "绿茶 3.5元\n"
     ]
    }
   ],
   "source": [
    "# import random\n",
    "import time\n",
    "goods_list = [\n",
    "    [\"火鸡面\",6元]]\n",
    "\n",
    "shopping_car = []\n",
    "\n",
    "money = input(\"请输入您支付宝余额：\")\n",
    "print(\"请选择你喜欢的商品：\")\n",
    "\n",
    "wait_time = random.randint(1,2)\n",
    "time.sleep (wait_time)\n",
    "\n",
    "goods = \"商品列表:\\n1.火鸡面 6元\\n2.王中王 2元\\n3.爽口豆干 1.5元\\n4.百事可乐 3.5元\\n5.可口可乐 3.5元\\n6.冰红茶 3元\\n7.绿茶 3.5元\"\n",
    "print(goods) \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3901592e",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e10048e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-10-25T07:55:57.074033Z",
     "start_time": "2021-10-25T07:55:48.246784Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您支付宝余额：33333\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：4\n",
      "【可口可乐】已经加入您的购物车，您的余额还有{33329.5}元\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：5\n",
      "【冰红茶】已经加入您的购物车，您的余额还有{33326.5}元\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：56\n",
      "您输入的商品不存在，请重新输入\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：6\n",
      "【绿茶】已经加入您的购物车，您的余额还有{33323.0}元\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：7\n",
      "您输入的商品不存在，请重新输入\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：4\n",
      "【可口可乐】已经加入您的购物车，您的余额还有{33319.5}元\n",
      "商品编号：0,商品名称：火鸡面,商品价格：6\n",
      "商品编号：1,商品名称：王中王,商品价格：2\n",
      "商品编号：2,商品名称：爽口豆干,商品价格：1.5\n",
      "商品编号：3,商品名称：百事可乐,商品价格：3.5\n",
      "商品编号：4,商品名称：可口可乐,商品价格：3.5\n",
      "商品编号：5,商品名称：冰红茶,商品价格：3\n",
      "商品编号：6,商品名称：绿茶,商品价格：3.5\n",
      "购买商品的编号（如要退出请按“q”）：q\n",
      "您的购物车： [['可口可乐', 3.5], ['冰红茶', 3], ['绿茶', 3.5], ['可口可乐', 3.5]]\n",
      "您的账户余额：33319.5\n"
     ]
    }
   ],
   "source": [
    "#===============================目标一、 录入商品================================\n",
    "good_list=[\n",
    "    [\"火鸡面\",6],#商品的样式：[商品名称，商品价格]\n",
    "    [\"王中王\",2],\n",
    "    [\"爽口豆干\",1.5],\n",
    "    [\"百事可乐\",3.5],\n",
    "    [\"可口可乐\",3.5],\n",
    "    [\"冰红茶\",3],\n",
    "    [\"绿茶\",3.5],\n",
    "]\n",
    "#------------写到某一步发现前面需要内容优化增加\n",
    "shopping_list = []#用户列表初始为空，后续使用append\n",
    "\n",
    "\n",
    "#录入支付宝余额\n",
    "money = int(input(\"请输入您支付宝余额：\"))#可用手段获取\n",
    "while True:\n",
    "\n",
    "    #写出商品单【打印商品信息】\n",
    "    #---------------------------------：错误部分：----------------------------------------------\n",
    "    # shopping_list = []\n",
    "\n",
    "    # lists = '''---------- list of shopping ----------'''\n",
    "    # for number in good_list:\n",
    "    #     \"商品编号：%s,商品名称：%s,商品价格：%s \n",
    "\n",
    "    # -----------------------------------修改----------------------------------------------\n",
    "    for i in range(len(good_list)):\n",
    "        #先写格式化的样式\n",
    "        print(\"商品编号：%s,商品名称：%s,商品价格：%s\"%(i,good_list[i][0],good_list[i][1]))\n",
    "\n",
    "\n",
    "    #=================================目标二：=============================\n",
    "    #让用户输入商品编号进行购买\n",
    "    choice = input(\"购买商品的编号（如要退出请按“q”）：\")\n",
    "    if choice.isdigit():#isdigit判断是否是数值\n",
    "        choice = int(choice)\n",
    "\n",
    "    #判断：\n",
    "        if choice >= len(good_list):\n",
    "            print(\"您输入的商品不存在，请重新输入\")\n",
    "        else:\n",
    "            #两件事情【用户余额商品价格关系】\n",
    "            if money >= good_list[choice][1]:\n",
    "                money = money - good_list[choice][1]\n",
    "                shopping_list.append(good_list[choice])\n",
    "                print(\"【%s】已经加入您的购物车，您的余额还有{%s}元\"%(good_list[choice][0],money))\n",
    "            else:\n",
    "                print(\"您的余额不足以购买，请更换支付方式\")\n",
    "    elif choice == \"q\":\n",
    "        #如果输入q则说明用户要退出购买了，我们将打印用户已购买了的商品列表与用户余额\n",
    "        print(\"您的购物车：\",shopping_list)\n",
    "        print(\"您的账户余额：%s\"%(money))\n",
    "        break\n",
    "    else:\n",
    "        print(\"请输入正确的【商品编码】，或者按照输入提示进行输入！\")\n",
    "\n",
    "\n",
    "#print"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
